﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

using Enterprise.Infrastructure;
using Enterprise.Model.Department;
using Enterprise.Data.Department;
namespace Enterprise.Service.Department
{
    /// <summary>
    /// 组织机构业务服务
    /// </summary>
    public class DepartmentService
    {
        /// <summary>
        ///  组织机构缓存标记
        /// </summary>
        private static string cacheKey = "Enterprise-Department";

        /// <summary>
        /// 组织机构列表 返回所有组织机构
        /// </summary>
        /// <returns></returns>
        public List<DepartmentEntity> DepartmentList()
        {
            if (CacheHelper.Contains(cacheKey))
            {
                return (List<DepartmentEntity>)CacheHelper.GetCache(cacheKey);
            }
            else
            {
                DepartmentData data = new DepartmentData();
                List<DepartmentEntity> list = data.DepartmentList().OrderBy(p=>p.DORDERBY).ToList();
                CacheHelper.Add(cacheKey, list);
                return list;
            }
        }

        /// <summary>
        /// 组织机构记录 返回组织机构记录信息
        /// </summary>
        /// <param name="deptId">组织机构编码</param>
        /// <returns></returns>
        public DepartmentEntity DepartmentDisp(int  deptId)
        {
            return DepartmentList().Where(p => p.DEPTID == deptId).FirstOrDefault();
        }

        /// <summary>
        /// 组织机构上移操作
        /// </summary>
        /// <param name="entity"></param>
        public void DepartmentMoveUp(int Id)
        {
            var entity = DepartmentDisp(Id);
            if (entity != null)
            {
                var upentity = DepartmentList().Where(p => p.DPARENTID == entity.DPARENTID && p.DORDERBY < entity.DORDERBY).OrderByDescending(p=>p.DORDERBY).FirstOrDefault();
                if (upentity != null)
                {
                    string upcmd = "Update SYS_DEPARTMENT set DORDERBY=" + entity.DORDERBY + "where DEPTID=" + upentity.DEPTID;
                    DepartmentExecuteBySql(upcmd, 0);
                    string cmd = "Update SYS_DEPARTMENT set DORDERBY=" + upentity.DORDERBY + " where DEPTID=" + entity.DEPTID;
                    DepartmentExecuteBySql(cmd, 0);
                    ClearCache();
                }
            }
        }

        private void ClearCache()
        {
            CacheHelper.RemoveCache(cacheKey);
        }

        /// <summary>
        /// 取得当前部门下最大的部门排序
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        public int GetMaxDepartmentOrderBy(int deptId)
        {
            var q = DepartmentList().Where(p => p.DPARENTID == deptId).OrderByDescending(p => p.DORDERBY).FirstOrDefault();
            if (q != null)
            {
                return q.DORDERBY;
            }
            return 0;
        }

        /// <summary>
        /// 组织机构下移操作
        /// </summary>
        /// <param name="Id"></param>
        public void DepartmentMoveDown(int Id)
        {
            var entity = DepartmentDisp(Id);
            if (entity != null)
            {
                var downentity = DepartmentList().Where(p => p.DPARENTID == entity.DPARENTID && p.DORDERBY > entity.DORDERBY).OrderBy(p => p.DORDERBY).FirstOrDefault();
                if (downentity != null)
                {
                    string upcmd = "Update SYS_DEPARTMENT set DORDERBY=" + entity.DORDERBY + "where DEPTID=" + downentity.DEPTID;
                    DepartmentExecuteBySql(upcmd, 0);
                    string cmd = "Update SYS_DEPARTMENT set DORDERBY=" + downentity.DORDERBY + " where DEPTID=" + entity.DEPTID;
                    DepartmentExecuteBySql(cmd, 0);
                    ClearCache();
                }
            }
        }

        /// <summary>
        /// 组织机构 添加、修改、删除
        /// </summary>
        /// <param name="entity">组织机构实体</param>
        public void DepartmentExecute(DepartmentEntity entity)
        {
            string cmd = string.Empty;
            switch (entity.DB_Option_Action)
            {
                case "Insert":
                    cmd = "Insert into SYS_DEPARTMENT(DNAME,DPARENTID,DDEPTH,DROOTID,DMANAGER,DHEADERMANAGER,DORDERBY) values('" + entity.DNAME + "'," + entity.DPARENTID + "," + entity.DDEPTH + "," + entity.DROOTID + "," + entity.DMANAGER + "," + entity.DHEADERMANAGER + "," + entity.DORDERBY + ")";
                    break;
                case "Update":
                    cmd = "Update SYS_DEPARTMENT set DNAME='" + entity.DNAME + "' , DPARENTID=" + entity.DPARENTID + " , DDEPTH=" + entity.DDEPTH + " , DROOTID=" + entity.DROOTID + ",DMANAGER=" + entity.DMANAGER + ",DHEADERMANAGER=" + entity.DHEADERMANAGER + " , DORDERBY=" + entity.DORDERBY + " where DEPTID=" + entity.DEPTID;
                    break;
                case "Delete":
                    cmd = "Delete from SYS_DEPARTMENT where DEPTID=" + entity.DEPTID;
                    break;
            }
            if (!string.IsNullOrEmpty(cmd))
            {
                DepartmentExecuteBySql(cmd,1);
            }
        }

        /// <summary>
        /// 组织机构操作
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="typeId">1更新 0不更新</param>
        public void DepartmentExecuteBySql(string cmd,int typeId)
        {
            DepartmentData data = new DepartmentData();
            data.DepartmentExecute(cmd);
            if (typeId == 1)
            {
                CacheHelper.RemoveCache(cacheKey);
            }
        }

        #region "返回部门名称"
        /// <summary>
        /// 返回部门名称
        /// </summary>
        /// <param name="deptId">部门编号</param>
        /// <returns></returns>
        public static string GetDepartMentName(int deptId)
        {
            DepartmentService dService = new DepartmentService();
            var q = dService.DepartmentDisp(deptId);
            if (q != null)
            {
                return q.DNAME;
            }
            return "";
        }

        /// <summary>
        /// 绑定部门下拉菜单
        /// </summary>
        /// <param name="ddl"></param>
        public static void BindDepartMentDropDownList(DropDownList ddl)
        {
            DepartmentService dService = new DepartmentService();
            List<DepartmentEntity> list = dService.DepartmentList().ToList();
            ddl.DataSource = list;
            ddl.DataTextField = "DNAME";
            ddl.DataValueField = "DEPTID";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("请选择...", "0"));
        }
        #endregion

    }
}
